package fpt.edu.vn.action;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import fpt.edu.vn.dao.AddnewDao;
import fpt.edu.vn.dao.TopicsDao;
import fpt.edu.vn.dao.UsersDao;
import fpt.edu.vn.model.Activityhistorys;
import fpt.edu.vn.model.Users;

/* 
 * AddUserAction.java 
 *  
 * 0.1
 * 
 * 10/15/2012 
 *  
 * Copyright notice 
 *  
 * Modification Logs: 
 *   DATE        AUTHOR      DESCRIPTION 
 *  -------------------------------------------------------- 
 *  15-Oct-2012  ThuongND    Create new 
 */
public class AddUserAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private String email;
	private String displayName;
	private String avatar;
	private int userIdss;
	private String errorString;
	private List<Users> users;
	private Users user = new Users();
	private AddnewDao addnewdao = new AddnewDao();
	private Activityhistorys activityhistory = new Activityhistorys();
	private UsersDao dao = new UsersDao();
	private String curUrl;
	private InputStream inputStream;
	private TopicsDao daot = new TopicsDao();
	// For SessionAware
	Map<String, Object> session;
	public String getCurrentUrl(){
		session = ActionContext.getContext().getSession();
		if(curUrl!=null)
			session.put("curUrl", curUrl);
		
		inputStream = toUTF8InputStream("OKie");
		
		return SUCCESS;
	}
	public static InputStream toUTF8InputStream(String str) {
		InputStream is = null;
		try {
			is = new ByteArrayInputStream(str.getBytes("UTF-8"));
		} catch (UnsupportedEncodingException e) {
			// UTF-8 should always be supported
			throw new AssertionError();
		}
		return is;
	}
	public String execute() {
		
		HttpServletRequest request = ServletActionContext.getRequest();
		request.getHeader("VIA");
		String ipAddress = request.getHeader("X-FORWARDED-FOR");
		if (ipAddress == null) {
			ipAddress = request.getRemoteAddr();
		}
		session = ActionContext.getContext().getSession();
		if(session.containsKey("curUrl")){
			curUrl = session.get("curUrl").toString();
		}
		
		if(curUrl==null){
			curUrl = "index.action";
		}
		session.put("ipaddress", (String) ipAddress);

		if ("undefined".equals(email)) {
			return ERROR;
		} else {
			if (dao.emailSearch(email)) {
				user.setAvatar(avatar);
				user.setEmail(email);
				user.setDisplayName(displayName);
				user.setRoleId(2);
				user.setCreateDate(getSysdate());
				user.setLastLoginDate(getSysdate());
				user.setIsBanner(0);
				dao.add(user);
			}
			this.users = addnewdao.getUser_Id(email);
			if (users.size() > 0) {
				setUserIdss(this.users.get(0).getUsersId());
				setDisplayName(this.users.get(0).getDisplayName());

			}
			if(this.users.get(0).getIsBanner()==0){
				session = ActionContext.getContext().getSession();
				session.put("displayNamess", (String) displayName);
				session.put("emailss", (String) email);
				session.put("userIdss", userIdss);
				session.put("userRoleIdss", users.get(0).getRoleId());	
			}else{
				errorString="Tài khoản của bạn đã bị khoá! ";
				return NONE;
			}
			dao.updateLastLogin(userIdss);
			activityhistory.setActivityTypeId(2);
			activityhistory.setIpAddress(ipAddress);
			activityhistory.setUserName(email);
			daot.add(activityhistory);

			return SUCCESS;
		}
	}

	public Date getSysdate() {
		Date date = new Date();
		return date;
	}

	public String getStrDate() {
		String curdate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
				.format(Calendar.getInstance().getTime());
		return curdate;
	}
	
	public InputStream getInputStream() {
		return inputStream;
	}
	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}
	public String getEmail() {
		return email;
	}
	
	public String getErrorString() {
		return errorString;
	}
	
	public String getCurUrl() {
		return curUrl;
	}

	public void setCurUrl(String curUrl) {
		this.curUrl = curUrl;
	}

	public void setErrorString(String errorString) {
		this.errorString = errorString;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getDisplayName() {
		return displayName;
	}

	public void setDisplayName(String displayName) {
		this.displayName = displayName;
	}

	public String getAvatar() {
		return avatar;
	}

	public void setAvatar(String avatar) {
		this.avatar = avatar;
	}

	public Users getUser() {
		return user;
	}

	public void setUser(Users user) {
		this.user = user;
	}

	public int getUserIdss() {
		return userIdss;
	}

	public void setUserIdss(int userIdss) {
		this.userIdss = userIdss;
	}

	public List<Users> getUsers() {
		return users;
	}

	public void setUsers(List<Users> users) {
		this.users = users;
	}

	public Activityhistorys getActivityhistory() {
		return activityhistory;
	}

	public void setActivityhistory(Activityhistorys activityhistory) {
		this.activityhistory = activityhistory;
	}
	

}
